﻿#region NameSpace
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Eb.StockManagement;
using Eb.MessageBox;
#endregion

public partial class Modules_StockManagement_StockIssue : System.Web.UI.UserControl
{
    #region Fields
    StockManagement stockManagement = new StockManagement();
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           // PopulateCategory();
            PopulateItemDetailsGrid();
           // PopulateOrderID();
           // txtReceiveDate.Text = DateReturn(DateTime.Now.ToString("MM-dd-yyyy"));
        }
    }
  
    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }



    protected void rbSpecificItem_CheckedChanged(object sender, EventArgs e)
    {
        //if (rbSpecificItem.Checked)
        //{
        //    up1.Visible = true;
        //    up2.Visible = false;
        //}
        //else
        //{
        //    up2.Visible = true;
        //    up1.Visible = false;
        //}
    }
    protected void rbSpecificItem0_CheckedChanged(object sender, EventArgs e)
    {
        //if (rbSpecificItem0.Checked)
        //{
        //    up2.Visible = true;
        //    up1.Visible = false;
        //}
        //else
        //{
        //    up1.Visible = true;
        //    up2.Visible = false;
        //}
    }
    protected void btnReceive_Click(object sender, EventArgs e)
    {

    }
 

    /// <summary>
    /// To chgeck all textboxes
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkAll = (CheckBox)grvProductSales.HeaderRow.FindControl("chkSelectAll");
        if (chkAll.Checked == true)
        {
            foreach (GridViewRow gvRow in grvProductSales.Rows)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
                chkSel.Checked = true;
                TextBox txtProductQty = (TextBox)gvRow.FindControl("txtQty");
                txtProductQty.ReadOnly = false;
                txtProductQty.ForeColor = System.Drawing.Color.Black;
                txtProductQty.BorderStyle = BorderStyle.Solid;
                txtProductQty.BackColor = System.Drawing.Color.White;
                txtProductQty.Font.Bold = true;
                gvRow.Font.Bold = true;
            }
        }
        else
        {
            foreach (GridViewRow gvRow in grvProductSales.Rows)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
                chkSel.Checked = false;
                TextBox txtProductQty = (TextBox)gvRow.FindControl("txtQty");
                txtProductQty.ReadOnly = true;
                txtProductQty.ForeColor = System.Drawing.Color.Black;
                txtProductQty.BackColor = System.Drawing.Color.Transparent;
                txtProductQty.BorderStyle = BorderStyle.None;
                txtProductQty.Text = "0";
                txtProductQty.Font.Bold = false;
                gvRow.Font.Bold = false;
            }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void chkSelect_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkTest = (CheckBox)sender;
        GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
        TextBox txtProductQty = (TextBox)grdRow.FindControl("txtQty");
        if (chkTest.Checked)
        {
            txtProductQty.BorderStyle = BorderStyle.Solid;
            txtProductQty.BackColor = System.Drawing.Color.White;
            txtProductQty.ReadOnly = false;
            txtProductQty.ForeColor = System.Drawing.Color.Navy;
            txtProductQty.Font.Bold = true;
            grdRow.ForeColor = System.Drawing.Color.Navy;
            grdRow.Font.Bold = true;
           // txtProductQty.ForeColor = System.Drawing.Color.Black;
        }
        else
        {
            txtProductQty.Text = "0";
            txtProductQty.ReadOnly = true;
            txtProductQty.ForeColor = System.Drawing.Color.Black;
            txtProductQty.BorderColor = System.Drawing.Color.Transparent;
            grdRow.ForeColor = System.Drawing.Color.Black;
            grdRow.Font.Bold = false;
            txtProductQty.Font.Bold = false;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            int userId = 0;
            if (Session["UserId"] != null)
                userId = Convert.ToInt16(Session["UserId"].ToString());

            if (CheckSelection())
            {
                if (grvProductSales.Rows.Count > 0)
                {
                    bool status = CheckQuantity();
                    if (!status)
                    {
                        MessageBox.Show("Invalid Quantity!!Not enough items in stock");
                    }
                    else
                    {
                        int LotID = stockManagement.InsertBatHistory(2, "", "", userId.ToString(), "", "", "");
                        bool salesStatus = stockManagement.InsertProductSales(LotID, txtReceiveDate.Text, txtWorkOredrID.Text, txtCustomerName.Text, txtAddress.Text, txtRemarks.Text);
                        foreach (GridViewRow gvRow in grvProductSales.Rows)
                        {
                            int itemId = 0;
                            int quantity = 0;
                            int quantity1 = 0;
                            CheckBox chkSelect = (CheckBox)gvRow.FindControl("chkSelect");
                            int productID = 0;
                            Label lblProductID = (Label)gvRow.FindControl("lblItemID");
                            if (lblProductID.Text.Trim() != "" || lblProductID.Text.Trim() != null)
                            {
                                productID = Convert.ToInt32(lblProductID.Text.Trim());
                            }
                            TextBox txtProductQty = (TextBox)gvRow.FindControl("txtQty");
                            Label lblCurrentStock = (Label)gvRow.FindControl("lblCurrentStock");
                            string qty = txtProductQty.Text;
                            if (qty != "" || qty != null)
                            {
                                quantity = -+Convert.ToInt16(qty);
                                quantity1 = Convert.ToInt16(qty);
                            }

                            if (chkSelect.Checked)
                            {
                                bool history=stockManagement.InsertHistory(LotID,productID,quantity1);
                                bool productStockStatus = stockManagement.UpdateCurrentAvailabilityOfStock(productID, quantity, "");
                                stockManagement.UpdateProductList(quantity, productID);
                                PopulateItemDetailsGrid();
                            }


                        }
                        MessageBox.Show("Successfully Added");
                    }
                }
            }
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Failed to add Quantity!";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }
    #endregion

    #region Methods
    //protected void PopulateOrderID()
    //{
    //   DataSet orderID= stockManagement.GetPurchaseOrderID();
    //   ddlOrderID.DataSource = orderID;
    //   ddlOrderID.DataValueField = "PurchaseRequestID";
    //   ddlOrderID.DataTextField = "PurchaseOrderID";

    //   ddlOrderID.DataBind();
    //   ddlOrderID.Items.Insert(0, "Select");
    //}
    //protected void PopulateCategory()
    //{
    //    ddlCategory.Items.Add("Products");
    //    ddlCategory.Items.Add("Materials");
    //   // ddlCategory.Items.Add("Accessories");
    //    ddlCategory.Items.Insert(0, "Select");
    //}

    /// <summary>
    /// Check  Required quantity > currectStock
    /// </summary>
    /// <returns></returns>
    protected bool CheckQuantity()
    {
        bool status = true;
        foreach (GridViewRow gvr in grvProductSales.Rows)
        {

              //CheckBox chkGrid = (CheckBox)(gvwUtilityGroup.Rows[i].Cells[0].FindControl("chkEditDelete"));
              //if (chkGrid.Checked == true)
              //{
              //}
            CheckBox chkSelect = (CheckBox)gvr.FindControl("chkSelect");
            int currentStock = 0;
            int requiredStock = 0;
            Label lblCurrentStock = (Label)gvr.FindControl("lblCurrentStock");
            currentStock = Convert.ToInt32(lblCurrentStock.Text);
            
            TextBox txtReqQty = (TextBox)gvr.FindControl("txtQty");
            requiredStock = Convert.ToInt32(txtReqQty.Text);
            if (chkSelect.Checked)
            {
                if (requiredStock > currentStock)
                {
                    status = false;
                    txtReqQty.BorderColor = System.Drawing.Color.Red;

                    //break;
                }
            }

        }
        return status;
    }

    /// <summary>
    /// Check the check box is selected /not
    /// </summary>
    /// <returns></returns>
    bool CheckSelection()
    {
        int count = 0;

        for (int i = 0; i < grvProductSales.Rows.Count; i++)
        {
            int requiredStock = 0;
            CheckBox chkGrid = (CheckBox)(grvProductSales.Rows[i].Cells[0].FindControl("chkSelect"));
            TextBox txtReqQty = (TextBox)(grvProductSales.Rows[i].Cells[4].FindControl("txtQty")); 
            requiredStock = Convert.ToInt32(txtReqQty.Text);

            if (chkGrid.Checked == true)
            {
                if (requiredStock <= 0)
                {
                    MessageBox.Show("You must enter the quantity greater than zero.");
                    return false;
                    break;
                }
                else
                    count++;
            }
        }

        if (count == 0)
        {
            MessageBox.Show("Please select a Product from the above table.");
            return false;
        }
        return true;
    }

    /// <summary>
    /// Populated Item Details
    /// </summary>
    protected void PopulateItemDetailsGrid()
    {
        DataSet itemDS = stockManagement.GetProductDtls();
        grvProductSales.DataSource = itemDS;
        grvProductSales.DataBind();

    }

    /// <summary>
    /// Get date
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    protected string DateReturn(string date)
    {
        string result = string.Empty;
        if (date.Contains("-"))
        {
            result = date.Replace("-", "/");
        }
        else
        {
            result = date;
        }
        return result;
    }
    #endregion
}
